package cn.neo.support.iv.fresco.photoview;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Locale;

import cn.neo.support.R;
import cn.neo.support.iv.fresco.XFresco;
import cn.neo.support.iv.fresco.listener.IDownloadResult;
import cn.neo.support.iv.fresco.photoview.anim.TransitionCompat;
import cn.neo.support.iv.fresco.photoview.entity.PhotoInfo;
import cn.neo.support.iv.fresco.photoview.photodraweeview.OnPhotoTapListener;

public class PictureBrowsePage extends FragmentActivity
        implements ViewPager.OnPageChangeListener, OnPhotoTapListener, View.OnLongClickListener, View.OnClickListener {
    private final static String TAG = PictureBrowsePage.class.getSimpleName();

    private int mPhotoIndex;
    private int mPhotoCount;
    private PhotoInfo mLookPhoto;

    private TextView tvPhotoIndex;
    private TextView tvPhotoDownLoad;
    private MViewPager mViewPager;

    private PictureBrowseAdapter mAdapter;
    private ArrayList<PhotoInfo> mItems;

    private TransitionCompat mTransitionCompat;
    private boolean mFirstExecAnimation;
    private boolean mIsAnimation;
    private boolean mIsLocal;

    Handler UIHandler = new Handler(Looper.getMainLooper());

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_picture_browse);

        Intent data = getIntent();
        mItems = data.getParcelableArrayListExtra(PictureBrowse.PHOTO_LIST_KEY);
        if (mItems == null || mItems.size() == 0) {
            Log.i("PictureBrowseActivity", "mItems is NULL");
            onBackPressed();
            return;
        }

        mPhotoIndex = data.getIntExtra(PictureBrowse.PHOTO_CURRENT_POSITION_KEY, 0);
        Log.i("PictureBrowseActivity", "mPhotoIndex = " + mPhotoIndex);
        mIsAnimation = data.getBooleanExtra(PictureBrowse.PHOTO_IS_ANIMATION_KEY, false);
        Log.i("PictureBrowseActivity", "isAnimation = " + mIsAnimation);
        mFirstExecAnimation = data.getBooleanExtra(PictureBrowse.PHOTO_ONLY_ONE_ANIMATION_KEY, false);
        Log.i("PictureBrowseActivity", "firstExecAnimation = " + mFirstExecAnimation);
        mIsLocal = data.getBooleanExtra(PictureBrowse.PHOTO_IS_LOCAL_KEY, false);
        Log.i("PictureBrowseActivity", "isLocal = " + mIsLocal);

        setupViews();

        if (mIsAnimation) {
            mTransitionCompat = new TransitionCompat(PictureBrowsePage.this);
            mTransitionCompat.setCurrentPosition(mPhotoIndex);
            mTransitionCompat.startTransition();
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        if (mFirstExecAnimation) {
            return;
        }

        mPhotoIndex = position;
        tvPhotoIndex.setText(String.format(Locale.getDefault(), "%d/%d", mPhotoIndex + 1, mPhotoCount));

        if (mTransitionCompat != null && mIsAnimation) {
            Log.i("PictureBrowseActivity", "onPageSelected mPhotoIndex = " + mPhotoIndex);
            mTransitionCompat.setCurrentPosition(mPhotoIndex);
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onBackPressed() {
        if (mTransitionCompat != null && mIsAnimation) {
            mTransitionCompat.finishAfterTransition();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public void onPhotoTap(View view, float x, float y) {
        onBackPressed();
    }

    private void setupViews() {
        tvPhotoIndex = (TextView) findViewById(R.id.tv_photo_count);
        tvPhotoDownLoad = (TextView) findViewById(R.id.tv_photo_download);

        mViewPager = (MViewPager) findViewById(R.id.vp_picture_browse);
        mViewPager.clearOnPageChangeListeners();
        mViewPager.addOnPageChangeListener(this);
        mAdapter = new PictureBrowseAdapter(mItems, this, this);
        mViewPager.setAdapter(mAdapter);

        mPhotoCount = mItems.size();
        if (mFirstExecAnimation) {
//            findViewById(R.id.rl_photo_bottom).setVisibility(View.GONE);
            tvPhotoIndex.setVisibility(View.GONE);
        } else {
            tvPhotoIndex.setText(String.format(Locale.getDefault(), "%d/%d", mPhotoIndex + 1, mPhotoCount));
        }
        if (mIsLocal) {
            tvPhotoDownLoad.setVisibility(View.GONE);
        } else {
            tvPhotoDownLoad.setOnClickListener(this);
        }

        mViewPager.setCurrentItem(mPhotoIndex);
    }

    @Override
    public void onClick(View v) {
        //下载图片
        PhotoInfo photoInfo = mItems.get(mPhotoIndex);
        XFresco.with(photoInfo.originalUrl)
                .setResult(new IDownloadResult(this) {
                    @Override
                    public void onResult(final String filePath) {
                        UIHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(PictureBrowsePage.this, "保存成功:" + filePath, Toast.LENGTH_SHORT).show();
                            }
                        });
                        Log.i(TAG, "SavePhoto::" + filePath);
                    }
                })
                .download();
    }

    @Override
    public boolean onLongClick(View view) {
        //下载图片
        return false;
    }

    @Override
    protected void onDestroy() {
        if (mLookPhoto != null) {
            mLookPhoto = null;
        }

        if (mItems != null) {
            mItems = null;
        }

        if (mAdapter != null) {
            mAdapter.recycler();
            mAdapter = null;
        }

        if (mViewPager != null) {
            mViewPager.removeOnPageChangeListener(this);
            mViewPager.setAdapter(null);
            mViewPager = null;
        }

        super.onDestroy();
    }


}
